/*! ******************************************************************************
 *
 * Created on 2022年3月16日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.core.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.*;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.core.PropsUI;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.trans.step.BaseStepDialog;

import java.io.*;
import java.nio.channels.FileChannel;
import java.security.NoSuchAlgorithmException;

/**
 * @author yzbdl@yzbdl.ac.cn
 * @version: 1.0
 * @Date 2021/6/21 9:55
 **/
public class DownLogDialog extends Dialog {
    private static Class<?> PKG = Spoon.class;

    private String dialogTitle;

    private Shell shell;
    private PropsUI props;
    private String inputLog;
    private String fileName = "";


    public DownLogDialog(Shell parent, String dialogTitle) {
        super(parent, SWT.NONE);
        props = PropsUI.getInstance();
        this.dialogTitle = dialogTitle;
    }

    protected Shell createShell(Shell parent) {
        return new Shell(parent, SWT.RESIZE | SWT.MAX | SWT.MIN);
    }

    public void open() throws NoSuchAlgorithmException, IOException, Exception {
        Shell parent = getParent();
        Display display = parent.getDisplay();

        //shell创建
        shell = createShell(parent);

                inputLog = getInputLogSrc();
                FileDialog dialog = new FileDialog(shell, SWT.OPEN);
                // 设置默认的路径
                dialog.setFilterPath("");
                //设置对话框的标题
                dialog.setText(BaseMessages.getString(PKG, "Help.downlog.downsrc.output"));
                //设置默认的文件名
                dialog.setFileName("");
                //设置扩展名
                dialog.setFilterNames(new String[]{"文本文件 (*.log)", "所有文件(*.*)"});
                //设置扩展名
                dialog.setFilterExtensions(new String[]{"*.log", "*.*"});
                String fileNameTemp = dialog.open();
                if (fileNameTemp != null) {
                    int i = fileNameTemp.indexOf(".log");
                    int j = fileNameTemp.indexOf(".txt");
                    //判断输入的文件名是否是文本类型
                    if (i == -1&&j==-1)
                        fileNameTemp += ".log";
                    fileName = fileNameTemp;
                    copyFileUsingRandomAccessFile(inputLog, fileName);
                    MessageBox msgBox = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
                    msgBox.setText(BaseMessages.getString(PKG, "Help.downlog.sucesss"));
                    msgBox.setMessage(BaseMessages.getString(PKG, "Help.downlog.sucesss"));
                    msgBox.open();
                }




        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }


    }

    public void dispose() {
        shell.dispose();
    }

    protected void cancel() throws Exception {
        dispose();
    }

    public String getInputLogSrc() {
        String property = System.getProperty("user.dir");
        String logSrc = property + "\\logs\\spoon.log";
        return logSrc;
    }

    /**
     * @param in  输入文件地址
     * @param out 输出文件地址
     * @throws IOException io异常
     */
    public static void copyFileUsingRandomAccessFile(String in, String out) {
        try {
            //读取文件
            RandomAccessFile input = new RandomAccessFile(in, "rw");
            //目标文件
            RandomAccessFile output = new RandomAccessFile(out, "rw");
            //判断是否超过10mb
            if (input.length() > 10 * 1024 * 1024) {
                byte[] arr = new byte[10 * 1024 * 1024];
                int len;
                //指针
                input.seek(input.length() - 10 * 1024 * 1024);
                while ((len = input.read(arr)) != -1) {
                    //写入
                    output.write(arr, 0, len);
                }
            }else {
                byte[] arr = new byte[(int)input.length()];
                int len;
                input.seek(0);
                while ((len = input.read(arr)) != -1) {
                    output.write(arr, 0, len);
                }
            }
            //关流
            input.close();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
